package cn.stu.flowable.controller;


import cn.stu.common.core.Constants;
import cn.stu.common.utils.R;
import cn.stu.flowable.domain.vo.FlowQueryVo;
import cn.stu.flowable.domain.vo.FlowTaskVo;
import cn.stu.flowable.service.IFlowTaskService;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 流任务控制器
 * 工作流任务管理
 *
 * @author Tony
 * @date 2021-04-03
 */
@Slf4j
@RestController
@RequestMapping("/flowable/task")
@AllArgsConstructor
public class FlowTaskController {

    /**
     * 流程任务服务
     */
    private IFlowTaskService flowTaskService;

    /**
     * 我流程
     *
     * @param queryVo queryVo
     * @return {@link R}
     */
    @GetMapping(value = "/myProcess")
    public R myProcess(FlowQueryVo queryVo) {
        return flowTaskService.myProcess(queryVo);
    }

    /**
     * 取消申请
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/stopProcess")
    public R stopProcess(@RequestBody FlowTaskVo flowTaskVo) {
        return flowTaskService.stopProcess(flowTaskVo);
    }

    /**
     * 撤销过程
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/revokeProcess")
    public R revokeProcess(@RequestBody FlowTaskVo flowTaskVo) {
        return flowTaskService.revokeProcess(flowTaskVo);
    }

    /**
     * 获取待办列表
     *
     * @param queryVo 向安理会主席提出问题
     * @return {@link R}
     */
    @GetMapping(value = "/todoList")
    public R todoList(FlowQueryVo queryVo) {
        return flowTaskService.todoList(queryVo);
    }

    /**
     * 获取已办任务
     *
     * @param queryVo 向安理会主席提出问题
     * @return {@link R}
     */
    @GetMapping(value = "/finishedList")
    public R finishedList(FlowQueryVo queryVo) {
        return flowTaskService.finishedList(queryVo);
    }


    /**
     * 流程历史流转记录
     *
     * @param procInsId 进程输入ID
     * @param deployId  部署ID
     * @return {@link R}
     */
    @GetMapping(value = "/flowRecord")
    public R flowRecord(String procInsId, String deployId) {
        return flowTaskService.flowRecord(procInsId, deployId);
    }


    /**
     * 流程初始化表单
     *
     * @param deployId 部署ID
     * @return {@link R}
     */
    @GetMapping(value = "/flowFormData")
    public R flowFormData(String deployId) {
        return flowTaskService.flowFormData(deployId);
    }

    /**
     * 获取流程变量
     *
     * @param taskId 任务ID任务ID
     * @return {@link R}
     */
    @GetMapping(value = "/processVariables/{taskId}")
    public R processVariables(@ApiParam(value = "流程任务Id") @PathVariable(value = "taskId") String taskId) {
        return flowTaskService.processVariables(taskId);
    }

    /**
     * 审批任务
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/complete")
    public R complete(@RequestBody FlowTaskVo flowTaskVo) {
        return flowTaskService.complete(flowTaskVo);
    }

    /**
     * 驳回任务
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/reject")
    public R<String> taskReject(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.taskReject(flowTaskVo);
        return R.ok(Constants.OPERATION_SUCCESS);
    }

    /**
     * 退回任务
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/return")
    public R<String> taskReturn(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.taskReturn(flowTaskVo);
        return R.ok(Constants.OPERATION_SUCCESS);
    }

    /**
     * 获取所有可回退的节点
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/returnList")
    public R findReturnTaskList(@RequestBody FlowTaskVo flowTaskVo) {
        return flowTaskService.findReturnTaskList(flowTaskVo);
    }

    /**
     * 删除
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @DeleteMapping(value = "/delete")
    public R<String> delete(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.deleteTask(flowTaskVo);
        return R.ok();
    }

    /**
     * 认领/签收任务
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/claim")
    public R<String> claim(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.claim(flowTaskVo);
        return R.ok(Constants.OPERATION_SUCCESS);
    }

    /**
     * 取消认领/签收任务
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/unClaim")
    public R<String> unClaim(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.unClaim(flowTaskVo);
        return R.ok(Constants.OPERATION_SUCCESS);
    }

    /**
     * 委派任务
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/delegateTask")
    public R<String> delegate(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.delegateTask(flowTaskVo);
        return R.ok(Constants.OPERATION_SUCCESS);
    }

    /**
     * 任务归还
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/resolveTask")
    public R<String> resolveTask(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.resolveTask(flowTaskVo);
        return R.ok(Constants.OPERATION_SUCCESS);
    }

    /**
     * 转办任务
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/assignTask")
    public R<String> assign(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.assignTask(flowTaskVo);
        return R.ok(Constants.OPERATION_SUCCESS);
    }

    /**
     * 添加多实例执行
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/addMultiInstanceExecution")
    public R<String> addMultiInstanceExecution(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.addMultiInstanceExecution(flowTaskVo);
        return R.ok("加签成功");
    }

    /**
     * 删除多实例执行
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/deleteMultiInstanceExecution")

    public R<String> deleteMultiInstanceExecution(@RequestBody FlowTaskVo flowTaskVo) {
        flowTaskService.deleteMultiInstanceExecution(flowTaskVo);
        return R.ok("减签成功");
    }

    /**
     * 获取下一个流节点
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/nextFlowNode")
    public R getNextFlowNode(@RequestBody FlowTaskVo flowTaskVo) {
        return flowTaskService.getNextFlowNode(flowTaskVo);
    }

    /**
     * 按开始获取下一个流节点
     *
     * @param flowTaskVo 流任务VO
     * @return {@link R}
     */
    @PostMapping(value = "/nextFlowNodeByStart")
    public R getNextFlowNodeByStart(@RequestBody FlowTaskVo flowTaskVo) {
        return flowTaskService.getNextFlowNodeByStart(flowTaskVo);
    }

    /**
     * 发电工艺流程图
     * 生成流程图
     *
     * @param processId 任务ID
     * @param response  响应
     */
    @GetMapping("/diagram/{processId}")
    public void genProcessDiagram(HttpServletResponse response,
                                  @PathVariable("processId") String processId) {
        InputStream inputStream = flowTaskService.diagram(processId);
        OutputStream os = null;
        BufferedImage image = null;
        try {
            image = ImageIO.read(inputStream);
            response.setContentType("image/png");
            os = response.getOutputStream();
            if (image != null) {
                ImageIO.write(image, "png", os);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取流查看器
     * 获取流程执行节点
     *
     * @param procInsId   流程实例编号
     * @param executionId 执行ID
     * @return {@link R}
     */
    @GetMapping("/flowViewer/{procInsId}/{executionId}")
    public R getFlowViewer(@PathVariable("procInsId") String procInsId,
                           @PathVariable("executionId") String executionId) {
        return flowTaskService.getFlowViewer(procInsId, executionId);
    }

    /**
     * 流XML和节点
     * 流程节点信息
     *
     * @param procInsId 流程实例id
     * @param deployId  部署ID
     * @return {@link R}
     */
    @GetMapping("/flowXmlAndNode")
    public R flowXmlAndNode(@RequestParam(value = "procInsId", required = false) String procInsId,
                            @RequestParam(value = "deployId", required = false) String deployId) {
        return flowTaskService.flowXmlAndNode(procInsId, deployId);
    }

    /**
     * 流程任务表
     * 流程节点表单
     *
     * @param taskId 流程任务编号
     * @return {@link R}
     * @throws Exception 例外情况
     */
    @GetMapping("/flowTaskForm")
    public R flowTaskForm(@RequestParam(value = "taskId", required = false) String taskId) throws Exception {
        return flowTaskService.flowTaskForm(taskId);
    }

}
